package com.eascs.ticket.configuration.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.exception.ServiceException;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.configuration.entity.TicketTypeStatus;
import com.eascs.ticket.configuration.entity.TicketTypeStatusUpdate;

@Service
public class TicketTypeStatusService extends BaseSevice {

	@Transactional(transactionManager = "ticketTransactionManager")
	public void updateTicketTypeStatus(TicketTypeStatusUpdate ticketTypeStatusUpdate) {
		this.proxyDao.updateById(ticketTypeStatusUpdate);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketTypeStatus(TicketTypeStatus ticketTypeStatus) {
		this.proxyDao.insert(ticketTypeStatus);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTicketTypeStatus(String ticketTypeStatusId) {
		this.proxyDao.deleteById(TicketTypeStatus.class, ticketTypeStatusId);
	}

	public TicketTypeStatus getTicketTypeStatusById(String ticketTypeStatusId) {
		return this.proxyDao.getById(TicketTypeStatus.class, ticketTypeStatusId);
	}

	public void checkNotUsedByTicketTypeCodeAndTicketStatusId(String ticketTypeCode, String ticketStatusId) {
		if (this.proxyDao.findInteger("select count(1) from ticket where ticket_type_code=? and status=?", ticketTypeCode, ticketStatusId) > 0) {
			throw new ServiceException("该状态正在被使用，无法删除");
		}
	}

	public List<Object[]> getTicketTypeStatusByTicketTypeCode(String ticketTypeCode) {
		return this.proxyDao.findObjectsList("select id,current_status,next_status,name,after_assign_type,comments,order_code from ticket_type_status where ticket_type_code=? order by order_code", ticketTypeCode);
	}

	public List<TicketTypeStatus> getTicketTypeStatusByTicketTypeCodeAndStatus(String ticketTypeCode, String status) {
		TicketTypeStatus ticketTypeStatus = new TicketTypeStatus();
		ticketTypeStatus.setTicketTypeCode(ticketTypeCode);
		ticketTypeStatus.setCurrentStatus(status);
		return this.proxyDao.findEntitysWithAny(ticketTypeStatus);
	}

	public List<Object[]> getStatusChangeDataSource() {
		return this.proxyDao.findObjectsList("sql.data.status_from_to");
	}

}
